MODULE boots
  USE GLOBVAR
  USE RANDOM
  USE STATISTICS
  USE MATRIX
  IMPLICIT NONE
  
CONTAINS
  
  SUBROUTINE boot(momentboot)
    IMPLICIT NONE
    REAL(8), INTENT(INOUT):: momentboot(:,:) !weight(:,:) !initial guess
    REAL(8) :: a(N_ind,n_testf),momb(n_boot,n_mom),momb2(n_boot,n_mom2),wei(n_mom2,n_mom2)
    REAL(8) :: inter(n_ind,n_levelf,6,3),interf(n_ind,n_levelf,3)
    REAL(8),ALLOCATABLE:: AVSE(:),cor(:,:)
    INTEGER :: duf(n_ind,n_levelf),du(n_levelf),ind(N_ind,n_levelf),vindex(n_vnum+50) 
    INTEGER :: i,j,k,t,m,n,l,indexf(n_testf,2), artf(n_ind,n_levelf,6),mm(n_ind),ii,jj,tt,kk,mmm 
   
    
    !!!!! start to calculate data moments!!!!!
    IF (myid==0) THEN
    !!! language tasks!!!!
   DO ii=1,n_boot     
       jj=ii
        CALL SET_SEED(1438914432,234621+jj,1377777,123727)
                j=0
                k=0
                t=0
              DO i=1,N_vnum+50
                       vindex(i)=INT(SAMPLE_UNIFORM(1.0D0, DBLE(N_vnum+1)))
                       DO l=1,n_ind
                           IF (vid(l)==vindex(i)) t=l
                       END DO
                       j=k+vsum(t)
                       DO kk=k+1,j
                         IF (kk<=n_ind) mm(kk)=id_min(t)+j-kk
                          If(kk>n_ind) GO TO 36
                       END DO
                      k=j
             END DO
        !write(*,*) 
        !stop
36     DO i=1,N_ind

           mmboot(ii,i)=mm(i)
       END DO       
       
        !!! fine tasks!!!!
    indexf=0
    DO i=1,N_ind
      DO j=1,N_testf
         IF (f_test(mm(i),j)>-90) THEN
             indexf(j,1)=indexf(j,1)+1
             IF (f_test(mm(i),j)>0.5) indexf(j,2)=indexf(j,2)+1
         END IF
      END DO
    END DO
    
    DO i=1,N_testf
        IF (indexf(i,1)>0.5) THEN
            momb(ii,i)=DBLE(indexf(i,2))/DBLE(indexf(i,1)) ! PASSING RATE FOR EACH Cognitive TASK
        ELSE 
            momb(ii,i)=-99
        END IF
    END DO
    
    !!!! generate artf variables !!!!
    artf=-1
    DO i=1,N_ind
        DO j=1,N_testf
            IF (f_test(mm(i),j)>-90) THEN
                k=levelf(j)
                m=repf(mm(i),j)
                artf(i,k,m)=f_test(mm(i),j)
                inter(i,k,m,1:3)=x_testf(mm(i),j,1:3)
            END IF
        END DO
    END DO
    
    !!!! passing rate at each level!!!!

    DO j=N_testf+1,N_testf+N_levelf
       m=0
       n=0 
       DO i=1,N_ind
           DO k=1,6
               IF (artf(i,j-N_testf,k)>-0.5) THEN
                   m=m+1
                   IF (artf(i,j-N_testf,k)==1) n=n+1
               END IF
           END DO 
       END DO
       IF (m>0) THEN
           momb(ii,j)=DBLE(n)/DBLE(m)
       ELSE
           momb(ii,j)=-99
       END IF
    END DO
    
    !!!! correlation across levels !!!!
    
    mmm=N_testf+N_levelf
    
    l=0 
   DO j=1,N_levelf-1
       IF (j==1) THEN
           tt=5
           kk=2
       ELSE IF (j==2) THEN
           tt=2
           kk=5
       ELSE IF (j==3) THEN
           tt=5
           kk=5
       ELSE IF (j==4) THEN
           tt=5
           kk=3
       ELSE IF(j==5) THEN
           tt=3
           kk=4
       ELSE IF (j==6) THEN
           tt=4
           kk=3
       END IF
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j+1,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j+1,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
   END DO
    
    mmm=N_testf+N_levelf+84
    
    l=0
    DO j=1,N_levelf
        
       IF (j==1) THEN
           tt=4
           kk=5
       ELSE IF (j==2) THEN
           tt=1
           kk=2
       ELSE IF (j==3) THEN
           tt=4
           kk=5
       ELSE IF (j==4) THEN
           tt=4
           kk=5
       ELSE IF(j==5) THEN
           tt=2
           kk=3
       ELSE IF (j==6) THEN
           tt=3
           kk=4
       ELSE IF (j==7) THEN
           tt=2
           kk=3
       END IF
        
      DO t=1,tt
        DO k=t+1,kk
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
    END DO
    
     mmm=N_testf+N_levelf+84+43
     
    l=0
    DO j=1,N_levelf-2
        IF (j==1) THEN
            tt=5
            kk=5
        ELSE IF (j==2) THEN
            tt=2
            kk=5
        ELSE IF (j==3) THEN
            tt=5
            kk=3
        ELSE IF (j==4) THEN
            tt=5
            kk=4
        ELSE IF (j==5) THEN
            tt=3
            kk=3
        END IF
        
      DO t=1,tt
        DO k=1,kk
          m=0
          n=0 
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)==1 .and. artf(i,j+2,k)>-0.5) THEN    ! the probability of passing the kth task at level j+1 conditional on passing the tth task at level j
                 m=m+1
                 IF (artf(i,j,t)==1 .and. artf(i,j+2,k)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
        END DO
      END DO
    END DO  
    
     mmm=N_testf+N_levelf+84+43+79
    
    
    indexf=0
    DO i=1,N_ind
      DO j=1,N_testf
          a(i,j)=firstf(mm(i),2)-monthf(j)-DBLE(timef(j))/4.0d0
         IF (f_test(mm(i),j)>-90 .and. a(i,j)>-1.25d0) THEN
             indexf(j,1)=indexf(j,1)+1
             IF (f_test(mm(i),j)>0.5) indexf(j,2)=indexf(j,2)+1
         END IF
      END DO
    END DO
    
    DO i=1,N_testf
        IF (indexf(i,1)>0.5) THEN
            momb(ii,mmm+i)=DBLE(indexf(i,2))/DBLE(indexf(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,mmm+i)=-99
        END IF
    END DO
    
    mmm=2*N_testf+N_levelf+84+43+79
    
    indexf=0
    DO i=1,N_ind
      DO j=1,N_testf
          a(i,j)=DBLE(firstf(mm(i),2)-monthf(j)-DBLE(timef(j))/4.0d0)
         IF (f_test(mm(i),j)>-90 .and. a(i,j)<-1.25d0) THEN
             indexf(j,1)=indexf(j,1)+1
             IF (f_test(mm(i),j)>0.5) indexf(j,2)=indexf(j,2)+1
         END IF
      END DO
    END DO

    
    
    DO i=1,N_testf
        IF (indexf(i,1)>0.5) THEN
            momb(ii,mmm+i)=DBLE(indexf(i,2))/DBLE(indexf(i,1)) ! PASSING RATE FOR EACH LANGUAGE TASK
        ELSE 
            momb(ii,mmm+i)=-99
        END IF
    END DO
    
    mmm=3*N_testf+N_levelf+84+43+79
    
    l=0
    DO j=1,N_levelf-1
      DO t=1,5
          m=0
          n=0
          l=l+1
          DO i=1,N_ind 
             IF (artf(i,j,t)>-0.5) THEN   
                 m=m+1
                 IF (artf(i,j,t)==1) n=n+1
             END IF
          END DO
          
          IF (m>0) THEN
             momb(ii,mmm+l)=DBLE(n)/DBLE(m)
          ELSE
             momb(ii,mmm+l)=-99
          END IF
      END DO
    END DO
    mmm=3*N_testf+N_levelf+84+43+79+5*(N_levelf-1)  
    
    DUf=-99
    ind=-99
    DO i=1,N_ind
        DO k=1,N_levelf
            DO j=1,6
                IF (artf(i,k,j)==1 .and. j==1) THEN
                    duf(i,k)=1
                    ind(i,k)=1
                ELSE IF (artf(i,k,j)==0 .and. j==1) THEN
                    duf(i,k)=j
                    ind(i,k)=0
                ELSE IF (j>1 ) THEN
                    IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==0 .and. ind(i,k)==0) THEN
                      duf(i,k)=j
                       ind(i,k)=0
                       IF (j<6  ) THEN
                           IF (artf(i,k,j+1)==-1) THEN
                           duf(i,k)=j+1
                           ind(i,k)=1
                           END IF
                       END IF                      
                           
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==0 .and. ind(i,k)==1) THEN
                        duf(i,k)=duf(i,k)
                        ind(i,k)=1
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==1 .and. ind(i,k)==0) THEN
                        duf(i,k)=j
                        ind(i,k)=1
                    ELSE IF (artf(i,k,j-1)==0 .and. artf(i,k,j)==1 .and. ind(i,k)==1) THEN
                        duf(i,k)=duf(i,k)
                        ind(i,k)=1
                    END IF
                END IF
            END DO           
                
        END DO
    END DO
    
    DO k=1,n_levelf
        m=0
        n=0
        DO i=1,n_ind
            IF (duf(i,k)>-90) THEN
               m=m+1
               n=n+duf(i,k)
            END IF
        END DO
        IF (m>0) THEN
          momb(ii,mmm+k)=DBLE(n)/DBLE(m)
        ELSE 
           momb(ii,mmm+k)=-99
        END IF
    END DO
    
    mmm=3*N_testf+N_levelf+84+43+79+5*(N_levelf-1)+n_levelf  
    
    du=0
    DO k=1,n_levelf
        DO i=1,N_ind
            IF (duf(i,k)>-90) THEN
                du(k)=du(k)+1
            END IF
        END DO
    END DO
    DO k=1,n_levelf
        allocate(avse(du(k)))
        m=0
        DO i=1,n_ind
            IF (duf(i,k)>-90) THEN
                m=m+1
                avse(m)=duf(i,k)
            END IF
        END DO
        momb(ii,mmm+k)=STDEV_V(AVSE)
        DEALLOCATE(avse)
    END DO
    mmm=3*N_testf+N_levelf+84+43+79+5*(N_levelf-1)+2*n_levelf 
    

    IND=0
    interf=0.0d0
    DO k=1,N_levelf
        DO i=1,N_ind
          DO j=1,6
              IF (inter(i,k,j,1)>-90) THEN
                  ind(i,k)=ind(i,k)+1
                  interf(i,k,1)=interf(i,k,1)+inter(i,k,j,1)
                  interf(i,k,2)=interf(i,k,2)+inter(i,k,j,2)
                  interf(i,k,3)=interf(i,k,3)+inter(i,k,j,3)
              END IF
          END DO
          IF (ind(i,k)>0) THEN
               interf(i,k,1)=DBLE(interf(i,k,1))/DBLE(ind(i,k))
               interf(i,k,2)=DBLE(interf(i,k,2))/DBLE(ind(i,k))
               interf(i,k,3)=DBLE(interf(i,k,3))/DBLE(ind(i,k))
          END IF
        END DO
    END DO
    
    
    du=0
    DO k=1,n_levelf
        DO i=1,N_ind
            IF (ind(i,k)>0 .AND. duf(i,k)>-90) THEN
            du(k)=du(k)+1          
            END IF
        END DO
    END DO
    
    DO k=1,n_levelf
        allocate(cor(du(k),4))
        m=0
        DO i=1,n_ind
             IF (ind(i,k)>0 .AND. duf(i,k)>-90) THEN
                m=m+1
                cor(m,1)=interf(i,k,1)
                cor(m,2)=interf(i,k,2)
                cor(m,3)=interf(i,k,3)
                cor(m,4)=duf(i,k)
             END IF
        END DO
        !IF (k>1) THEN
        momb(ii,mmm+3*(k-1)+1)=Correlation(cor(:,1),cor(:,4))
        momb(ii,mmm+3*(k-1)+2)=Correlation(cor(:,2),cor(:,4))
        momb(ii,mmm+3*(k-1)+3)=Correlation(cor(:,3),cor(:,4))
        !ELSE 
        !    momb(ii,mmm+3*(k-1)+1)=-99
        !    momb(ii,mmm+3*(k-1)+2)=-99
        !    momb(ii,mmm+3*(k-1)+3)=-99
        !END IF
        DEALLOCATE(cor)
    END DO
    
      
    
   END DO !ii
   
   momentboot=momb
   
       
           !
           !wei=Variance_Covariance(momb2)
           !!weight=Matrix_Inverse_symmetric(wei)
           !weight=Matrix_Inverse(wei)
           !OPEN(12,file="wei.out")
           !  DO j=1,N_mom2
           !   WRITE(12,'(2000F32.12)') (wei(j,i),i=1,n_mom2)
           !  END DO
           !CLOSE(12) 
           !OPEN(12,file="weight.out")
           !  DO j=1,N_mom2
           !   WRITE(12,'(2000F32.12)') (weight(j,i),i=1,n_mom2)
           !  END DO
           !CLOSE(12)  
           !OPEN(12,file="momm.out")
           !  DO i=1,N_mom
           !   WRITE(12,'(2000F32.12)') (momb(j,i),j=1,n_boot)
           !  END DO
           !CLOSE(12) 
              OPEN(12,file="mmboot.out")
             DO i=1,N_ind
              WRITE(12,'(2000I4)') (mmboot(j,i),j=1,n_boot)
             END DO
           CLOSE(12) 
      
    
    
    END IF  
        
    
    
    
    
  END SUBROUTINE boot
  
END MODULE boots